package cn.itcast.flink.batch;

import org.apache.commons.io.FileUtils;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.MapOperator;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilulu
 */

/**
 * * Flink 中批处理分布式缓存：将缓存数据文件数据，数据放在文件中；。
 */
public class BatchDistributedCacheDemo {
    public static void main(String[] args) throws Exception {
        // 1. 执行环境-env
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // todo step1. 将数据文件进行缓存，注意文件不能太大，属于小文件数据
        env.registerCachedFile("datas/distribute_cache_student", "cache_students");

        // 2. 数据源-source
        DataSource<Tuple3<Integer, String, Integer>> scoreDataSet = env.fromCollection(
                Arrays.asList(
                        Tuple3.of(1, "语文", 50),
                        Tuple3.of(1, "数学", 70),
                        Tuple3.of(1, "英语", 86),
                        Tuple3.of(2, "语文", 80),
                        Tuple3.of(2, "数学", 86),
                        Tuple3.of(2, "英语", 96),
                        Tuple3.of(3, "语文", 90),
                        Tuple3.of(3, "数学", 68),
                        Tuple3.of(3, "英语", 92)
                )
        );
        // 3. 数据转换-transformation
        MapOperator<Tuple3<Integer, String, Integer>, String> resultDataSet = scoreDataSet.map(new CacheMapFunction());
        // 4. 数据终端-sink
        resultDataSet.print();
        // 5. 触发执行-execute
//        env.execute("BatchDistributedCacheDemo");
    }

    private static class CacheMapFunction extends RichMapFunction<Tuple3<Integer, String, Integer>, String> {

        private Map<Integer, String> stuMap = new HashMap<>();

        @Override
        public void open(Configuration parameters) throws Exception {
            File file = getRuntimeContext().getDistributedCache().getFile("cache_students");
            List<String> list = FileUtils.readLines(file, Charset.defaultCharset());
            for (String line : list) {
                String[] split = line.split(",");
                stuMap.put(Integer.valueOf(split[0]), split[1]);
            }
        }

        @Override
        public String map(Tuple3<Integer, String, Integer> tuple3) throws Exception {
            String stuName = stuMap.getOrDefault(tuple3.f0, "未知");
            return stuName + "," + tuple3.f1 + "," + tuple3.f2;
        }
    }
}